package com.wind.framework.util;

/**
 * @author： BeiYan
 * @Date： 2016-10-15 15:58
 * @Description:
 */

public class CastUtil {
    public static String castString(Object object, String defaultString) {
        return object != null ? String.valueOf(object) : defaultString;
    }

    public static String castString(Object object) {
        String defaultValue = "";
        return castString(object, defaultValue);
    }

    public static long castLong(Object object) {
        return castLong(object, 0);
    }

    public static long castLong(Object object, long defaultValue) {
        long longValue = defaultValue;
        if (object != null) {
            String strValue = castString(object);
            if (StringUtil.isNotEmpty(strValue)) {
                try {
                    longValue = Long.parseLong(strValue);
                } catch (NumberFormatException e) {
                    longValue = defaultValue;
                }
            }
        }
        return longValue;
    }

    public static int castInt(Object object, int defaultValue) {
        int intValue = defaultValue;
        if (object != null) {
            String strValue = castString(object);
            if (StringUtil.isNotEmpty(strValue)) {
                try {
                    intValue = Integer.parseInt(strValue);
                } catch (NumberFormatException e) {
                    intValue = defaultValue;
                }
            }

        }
        return intValue;
    }

    public static int castInt(Object object) {
        int defaultValue = 0;
        return castInt(object, defaultValue);
    }

    public static boolean castBoolean(Object object, boolean defaultValue) {
        boolean booleanValue = defaultValue;
        if (object != null) {
            String strValue = castString(object);
            if (StringUtil.isNotEmpty(strValue)) {
                try {
                    booleanValue = Boolean.valueOf(strValue);
                } catch (NumberFormatException e) {
                    booleanValue = defaultValue;
                }
            }

        }
        return booleanValue;
    }

    public static boolean castBoolean(Object object) {
        boolean defaultValue = false;
        return castBoolean(object, defaultValue);
    }

    public static double castDouble(Object object) {
        double defaultValue = 0;
        return castDouble(object, defaultValue);
    }

    public static double castDouble(Object object, double defaultValue) {
        double doubleValue = defaultValue;
        if (object != null) {
            String strValue = castString(object);
            if (StringUtil.isNotEmpty(strValue)) {
                try {
                    doubleValue = Double.valueOf(strValue);
                } catch (NumberFormatException e) {
                    doubleValue = defaultValue;
                }
            }
        }
        return doubleValue;
    }
}

